package cn.lf.android.lawfirm.view;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import cn.lf.android.lawfirm.R;
import cn.lf.android.lawfirm.utils.DisplayUtil;


/**
 * 描述 : Dialog
 */
public class OptionDialog extends Dialog implements View.OnClickListener {

	private LinearLayout mCustomViewContainer;
	private LinearLayout mContentLayout;
	private View mCustomView;

	private ImageView mContentIv;

	private Bitmap mContentBitmap;

	private View mBtnGroupView, mDividerView, mBkgView, mDialogView;

	private TextView mTitleTv, mContentTv, mPositiveBtn, mNegativeBtn;

	private Drawable mDrawable;

	private AnimationSet mAnimIn, mAnimOut;

	private int mResId, mBackgroundColor, mTitleTextColor, mContentTextColor,mPositiveTextColor,mNegativeTextColor;

	private int mTitleTextSize, mContentTextSize,mPositiveTextSize,mNegativeTextSize;
	
	private OnPositiveListener mPositiveListener;

	private OnNegativeListener mNegativeListener;

	private CharSequence mTitleText, mContentText, mPositiveText,
			mNegativeText;

	private boolean mIsShowAnim;
	
	/**diaglog 根布局**/
	View contentView;
	
	public OptionDialog(Context context) {
		this(context, R.style.OptionDialog);
	}

	public OptionDialog(Context context, int theme) {
		super(context,theme );
		init();
	}
	

	private void callDismiss() {
		super.dismiss();
	}

	private void init() {
		mAnimIn = AnimationLoader.getInAnimation(getContext());
		mAnimOut = AnimationLoader.getOutAnimation(getContext());
		initAnimListener();
	}

	@Override
	public void setTitle(CharSequence title) {
		mTitleText = title;
	}

	@Override
	public void setTitle(int titleId) {
		setTitle(getContext().getText(titleId));
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Window window=getWindow();
		
		window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN |
				WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); 
		
		super.onCreate(savedInstanceState);
		contentView = View.inflate(getContext(),
				R.layout.optiondialog_option, null);
		setContentView(contentView);

		mDialogView = getWindow().getDecorView().findViewById(
				android.R.id.content);
		mBkgView = contentView.findViewById(R.id.llBkg);
		mTitleTv = (TextView) contentView.findViewById(R.id.tvTitle);
		mContentTv = (TextView) contentView.findViewById(R.id.tvContent);
		mContentIv = (ImageView) contentView.findViewById(R.id.ivContent);

		mPositiveBtn = (TextView) contentView.findViewById(R.id.btnPositive);
		mNegativeBtn = (TextView) contentView.findViewById(R.id.btnNegative);

		mDividerView = contentView.findViewById(R.id.divider);
		mBtnGroupView = contentView.findViewById(R.id.llBtnGroup);

		mCustomViewContainer = (LinearLayout) contentView
				.findViewById(R.id.custumView);
		mContentLayout = (LinearLayout) contentView
				.findViewById(R.id.llContent);

		mPositiveBtn.setOnClickListener(this);
		mNegativeBtn.setOnClickListener(this);

		if (mTitleText == null) {
			mTitleTv.setVisibility(View.GONE);
		} else {
			mTitleTv.setVisibility(View.VISIBLE);
			mTitleTv.setText(mTitleText);
		}
		mContentTv.setText(mContentText);
		mPositiveBtn.setText(mPositiveText);
		mNegativeBtn.setText(mNegativeText);

		if (null == mPositiveListener && null == mNegativeListener) {
			mBtnGroupView.setVisibility(View.GONE);
		} else if (null == mPositiveListener && null != mNegativeListener) {
			mPositiveBtn.setVisibility(View.GONE);
			mDividerView.setVisibility(View.GONE);
			mNegativeBtn.setBackgroundDrawable(getContext().getResources()
					.getDrawable(R.drawable.selector_colordlg_def_gray));
		} else if (null != mPositiveListener && null == mNegativeListener) {
			mNegativeBtn.setVisibility(View.GONE);
			mDividerView.setVisibility(View.GONE);
			mPositiveBtn.setBackgroundDrawable(getContext().getResources()
					.getDrawable(R.drawable.selector_colordlg_def_gray));
		}

		if (null != mDrawable) {
			mContentIv.setBackgroundDrawable(mDrawable);
		}

		if (null != mContentBitmap) {
			mContentIv.setImageBitmap(mContentBitmap);
		}

		if (0 != mResId) {
			mContentIv.setBackgroundResource(mResId);
		}

		setTextColor();

		setTextSize();
		
		setBackgroundColor();

		setContentMode();

		if (mCustomView != null) {
			mTitleTv.setVisibility(View.GONE);
			mContentLayout.setVisibility(View.GONE);
			mCustomViewContainer.addView(mCustomView);
			mCustomViewContainer.setVisibility(View.VISIBLE);
		}
	}

	@Override
	protected void onStart() {
		super.onStart();
		startWithAnimation(mIsShowAnim);
	}

	@Override
	public void dismiss() {
		dismissWithAnimation(mIsShowAnim);
	}

	/**
	 * 直接关闭不执行动画
	 */
	public void optionDialogDismiss() {
		super.dismiss();
	}

	private void startWithAnimation(boolean showInAnimation) {
		if (showInAnimation) {
			mDialogView.startAnimation(mAnimIn);
		}
	}

	private void dismissWithAnimation(boolean showOutAnimation) {
		if (showOutAnimation) {
			mDialogView.startAnimation(mAnimOut);
		} else {
			super.dismiss();
		}
	}

	private void initAnimListener() {
		mAnimOut.setAnimationListener(new Animation.AnimationListener() {
			@Override
			public void onAnimationStart(Animation animation) {
			}

			@Override
			public void onAnimationEnd(Animation animation) {
				mDialogView.post(new Runnable() {
					@Override
					public void run() {
						callDismiss();
					}
				});
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
			}
		});
	}

	private void setBackgroundColor() {

		if (0 == mBackgroundColor) {
			return;
		}

		int radius = DisplayUtil.dp2px(getContext(), 6);
		float[] outerRadii = new float[] { radius, radius, radius, radius, 0,
				0, 0, 0 };
		RoundRectShape roundRectShape = new RoundRectShape(outerRadii, null,
				null);
		ShapeDrawable shapeDrawable = new ShapeDrawable(roundRectShape);
		shapeDrawable.getPaint().setColor(mBackgroundColor);
		shapeDrawable.getPaint().setStyle(Paint.Style.FILL);
		mBkgView.setBackgroundDrawable(shapeDrawable);
	}

	private void setTextColor() {

		if (0 != mTitleTextColor) {
			mTitleTv.setTextColor(mTitleTextColor);
		}

		if (0 != mContentTextColor) {
			mContentTv.setTextColor(mContentTextColor);
		}

		if (0 != mPositiveTextColor) {
			mPositiveBtn.setTextColor(mPositiveTextColor);
		}
		
		if (0 != mNegativeTextColor) {
			mNegativeBtn.setTextColor(mNegativeTextColor);
		}
		
		
	}
	
	private void setTextSize(){
		if(0!=mTitleTextSize){
			mTitleTv.setTextSize(mTitleTextSize);
		}
		
		if(0!=mContentTextSize){
			mContentTv.setTextSize(mContentTextSize);
		}
		
		if(0!=mPositiveTextSize){
			mPositiveBtn.setTextSize(mPositiveTextSize);
		}
		
		if(0!=mNegativeTextSize){
			mNegativeBtn.setTextSize(mNegativeTextSize);
		}
	}

	private void setContentMode() {
		boolean isImageMode = (null != mDrawable | null != mContentBitmap | 0 != mResId);
		boolean isTextMode = (!TextUtils.isEmpty(mContentText));

		if (isImageMode && isTextMode) {
			FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) mContentTv
					.getLayoutParams();
			params.gravity = Gravity.BOTTOM;
			mContentTv.setLayoutParams(params);
			mContentTv.setBackgroundColor(Color.BLACK);
			mContentTv.getBackground().setAlpha(0x28);
			mContentTv.setVisibility(View.VISIBLE);
			mContentIv.setVisibility(View.VISIBLE);
			return;
		}

		if (isTextMode) {
			FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) mContentTv
					.getLayoutParams();
			params.gravity = Gravity.NO_GRAVITY;
			mContentTv.setLayoutParams(params);
			mContentIv.setVisibility(View.GONE);
			mContentTv.setVisibility(View.VISIBLE);
			return;
		}

		if (isImageMode) {
			mContentTv.setVisibility(View.GONE);
			mContentIv.setVisibility(View.VISIBLE);
			return;
		}

	}

	@Override
	public void onClick(View v) {
		int id = v.getId();

		if (R.id.btnPositive == id) {
			mPositiveListener.onClick(this);
		} else if (R.id.btnNegative == id) {
			mNegativeListener.onClick(this);
		} else {
		}
	}

	public OptionDialog setAnimationEnable(boolean enable) {
		mIsShowAnim = enable;
		return this;
	}

	public OptionDialog setAnimationIn(AnimationSet animIn) {
		mAnimIn = animIn;
		return this;
	}

	public OptionDialog setAnimationOut(AnimationSet animOut) {
		mAnimOut = animOut;
		initAnimListener();
		return this;
	}

	public OptionDialog setColor(int color) {
		mBackgroundColor = color;
		return this;
	}

	public OptionDialog setColor(String color) {
		try {
			setColor(Color.parseColor(color));
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		}
		return this;
	}

	public OptionDialog setTitleTextColor(int color) {
		mTitleTextColor = color;
		return this;
	}

	public OptionDialog setTitleTextColor(String color) {
		try {
			setTitleTextColor(Color.parseColor(color));
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		}
		return this;
	}

	public OptionDialog setContentTextColor(int color) {
		mContentTextColor = color;
		return this;
	}

	public OptionDialog setContentTextColor(String color) {
		try {
			setContentTextColor(Color.parseColor(color));
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		}
		return this;
	}
	
	public OptionDialog setPositiveTextColor(int color) {
		mPositiveTextColor = color;
		return this;
	}

	public OptionDialog setPositiveTextColor(String color) {
		try {
			setPositiveTextColor(Color.parseColor(color));
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		}
		return this;
	}
	
	public OptionDialog setNegativeTextColor(int color) {
		mNegativeTextColor = color;
		return this;
	}

	public OptionDialog setNegativeTextColor(String color) {
		try {
			setNegativeTextColor(Color.parseColor(color));
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		}
		return this;
	}
	

	public OptionDialog setTitleTextSize(int size){
		mTitleTextSize=size;
		return this;
	}
	
	public OptionDialog setContentTextSize(int size){
		mContentTextSize=size;
		return this;
	}
	
	public OptionDialog setPositiveTextSize(int size){
		mPositiveTextSize=size;
		return this;
	}
	
	public OptionDialog setNegativeTextSize(int size){
		mNegativeTextSize=size;
		return this;
	}
	
	public OptionDialog setPositiveListener(CharSequence text,
			OnPositiveListener l) {
		mPositiveText = text;
		mPositiveListener = l;
		return this;
	}

	public OptionDialog setPositiveListener(int textId, OnPositiveListener l) {
		return setPositiveListener(getContext().getText(textId), l);
	}

	public OptionDialog setNegativeListener(CharSequence text,
			OnNegativeListener l) {
		mNegativeText = text;
		mNegativeListener = l;
		return this;
	}

	public OptionDialog setNegativeListener(int textId, OnNegativeListener l) {
		return setNegativeListener(getContext().getText(textId), l);
	}

	public OptionDialog setContentText(CharSequence text) {
		mContentText = text;
		return this;
	}

	public OptionDialog setContentText(int textId) {
		return setContentText(getContext().getText(textId));
	}

	public OptionDialog setContentImage(Drawable drawable) {
		mDrawable = drawable;
		return this;
	}

	public OptionDialog setContentImage(Bitmap bitmap) {
		mContentBitmap = bitmap;
		return this;
	}

	public OptionDialog setContentImage(int resId) {
		mResId = resId;
		return this;
	}

	public CharSequence getContentText() {
		return mContentText;
	}

	public CharSequence getTitleText() {
		return mTitleText;
	}

	public CharSequence getPositiveText() {
		return mPositiveText;
	}

	public CharSequence getNegativeText() {
		return mNegativeText;
	}

	public interface OnPositiveListener {
		void onClick(OptionDialog dialog);
	}

	public interface OnNegativeListener {
		void onClick(OptionDialog dialog);
	}

	public TextView getmPositiveBtn() {
		return mPositiveBtn;
	}

	public TextView getmNegativeBtn() {
		return mNegativeBtn;
	}

	/**
	 * 设置自定义的布局，替换标题和内容部分
	 * 
	 * @param view
	 */
	public void setCustomView(View view) {
		mCustomView = view;
	}
	
	/**
	 * 获取自定义View的parent
	 * @return
	 */
	public View getCustomViewContainer(){
		return mCustomViewContainer;
	}
	/**
	 * 获取内容区rootview
	 * @return
	 */
	public View getContentRootView(){
		return mBkgView;
	}
	
	/**
	 * dialog 主佈局
	 * @return
	 */
	public LinearLayout getContentView(){
		return (LinearLayout) contentView;
	}
}
